package com.zxy.lm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxy.lm.VO.UserLoginVO;
import com.zxy.lm.VO.UserRegisterVO;
import com.zxy.lm.entity.BorrowRecord;
import com.zxy.lm.entity.User;
import com.zxy.lm.mapper.BookMapper;
import com.zxy.lm.mapper.BorrowRecordMapper;
import com.zxy.lm.mapper.UserMapper;
import com.zxy.lm.service.BorrowRecordService;
import com.zxy.lm.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jiusi
 * @since 2021-06-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BorrowRecordMapper borrowRecordMapper;

    @Autowired
    private BorrowRecordService borrowRecordService;

    @Autowired
    private BookMapper bookMapper;

    @Override
    public User login(UserLoginVO user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        queryWrapper.eq("password", user.getPassword());
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean register(UserRegisterVO userVO) {
        User user = new User();
        user.setUsername(userVO.getUsername())
                .setPassword(userVO.getPassword())
                .setEmail(userVO.getEmail())
                .setStatus(0)
                .setId(null);
        int insert = baseMapper.insert(user);
        return insert > 0;
    }

    @Override
    public Integer getUserIdByUsername(String username) {
        return userMapper.getUserIdByUsername(username);
    }

    @Override
    public boolean changePassword(String username, String pass, String newPass) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", pass);
        User user = userMapper.selectOne(queryWrapper);
        //找不到用户
        if (null == user) {
            return false;
        }
        user.setPassword(newPass);
        int i = userMapper.updateById(user);
        return i > 0;
    }

}
